package activity.setting;

import com.hichip.callback.ICameraIOSessionCallback;
import com.hichip.campro.R;
import com.hichip.content.HiChipDefines;
import com.hichip.control.HiCamera;
import com.hichip.sdk.HiChipP2P;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.InputFilter;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import base.HiActivity;
import bean.MyCamera;
import common.HiDataValue;
import common.TitleView;
import main.MainActivity;
import utils.EmojiFilter;
import utils.FullCharFilter;
import utils.HiToast;
import utils.HiTools;
import utils.SpcialCharFilterWIFISET;
/**
 * WIFI设置 二级界面
 * @author lt
 */
public class WifiSetToDeviceActivity extends HiActivity implements ICameraIOSessionCallback {
	private TitleView mTitleView;
	private EditText mEtPw;
	private TextView wifi_to_devide_application;
	private TextView mTvSsid;
	private String mSsid;
	private String mUid;
	private MyCamera mCamera;
	protected byte wifiSelectMode;
	protected byte wifiSelectEncType;
	protected boolean send;
	private boolean isSupportLenExt = false;
	private boolean isSupportFullChar=false;
	private byte[] mPassWord;

	@Override
	protected int setLayoutId() {
		return R.layout.activity_wifi_set_to_device;
	}

	@Override
	protected void init(@Nullable Bundle savedInstanceState) {
		getIntentData();
		initData();
		initView();
		setListerners();
	}

	private void getIntentData() {
		mSsid = getIntent().getStringExtra(HiDataValue.EXTRAS_KEY_DATA);
		mUid = getIntent().getStringExtra(HiDataValue.EXTRAS_KEY_UID);
		wifiSelectMode = getIntent().getByteExtra(WiFiSettingActivity.WIFISELECTMODE, (byte) 0);
		wifiSelectEncType = getIntent().getByteExtra(WiFiSettingActivity.WIFISELECTENCTYPE, (byte) 0);
		mPassWord = getIntent().getByteArrayExtra("password");
	}

	private void setListerners() {
		wifi_to_devide_application.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (mCamera == null) {
					return;
				}
				String ssid = mSsid.trim();
				String psw = mEtPw.getText().toString().trim();
				if (mCamera.getCommandFunction(HiChipDefines.HI_P2P_SET_WIFI_PARAM_NEWPWD255_EXT1)&&isSupportFullChar) {
					if (HiTools.isMaxLength(ssid, 32)) {
						HiToast.showToast(WifiSetToDeviceActivity.this, getString(R.string.toast_ssid_tolong));
						return;
					}
					boolean isMax = HiTools.isMaxLength(psw, 63);
					if (isMax) {
						HiToast.showToast(WifiSetToDeviceActivity.this, getString(R.string.tips_input_tolong));
						return;
					}
					mCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_WIFI_PARAM_NEWPWD255_EXT1, HiChipDefines.HI_P2P_S_WIFI_PARAM_NEWPWD255_EXT1.parseContent(HiChipP2P.HI_P2P_SE_CMD_CHN, 0, wifiSelectMode, wifiSelectEncType, ssid.getBytes(), psw.getBytes()));
				}else {
					if (psw.getBytes().length > 31) {
						HiToast.showToast(WifiSetToDeviceActivity.this, getString(R.string.tips_input_tolong));
						return;
					}
					if (ssid.getBytes().length > 31) {
						HiToast.showToast(WifiSetToDeviceActivity.this, getString(R.string.toast_ssid_tolong));
						return;
					}
					mCamera.sendIOCtrl(HiChipDefines.HI_P2P_SET_WIFI_PARAM, HiChipDefines.HI_P2P_S_WIFI_PARAM.parseContent(HiChipP2P.HI_P2P_SE_CMD_CHN, 0, wifiSelectMode, wifiSelectEncType, ssid.getBytes(), psw.getBytes()));
				}
				showjuHuaDialog();
				send = true;
				mHandler.sendEmptyMessageDelayed(WiFiSettingActivity.SET_WIFI_END, 10000);
			}
		});

	}

	private void initData() {
		for (MyCamera camera : HiDataValue.CameraList) {
			if (mUid.equals(camera.getUid())) {
				mCamera = camera;
				isSupportFullChar=mCamera.getCommandFunction(HiChipDefines.HI_P2P_GET_CHAR);
				isSupportLenExt = mCamera.getCommandFunction(HiChipDefines.HI_P2P_GET_WIFI_PARAM_NEWPWD255_EXT1);
				break;
			}
		}
	}

	private void initView() {
		mTitleView = (TitleView) findViewById(R.id.wifitode_title_top);
		mTitleView.setTitle(getString(R.string.wifi_setting));
		mTitleView.setButton(TitleView.NAVIGATION_BUTTON_LEFT);
		mTitleView.setNavigationBarButtonListener(new TitleView.NavigationBarButtonListener() {
			@Override
			public void OnNavigationButtonClick(int which) {
				switch (which) {
					case TitleView.NAVIGATION_BUTTON_LEFT:
						WifiSetToDeviceActivity.this.finish();
						break;
				}
			}
		});
		mEtPw = (EditText) findViewById(R.id.wifi_to_device_pw);

		if (mPassWord != null) {
			mEtPw.setTransformationMethod(PasswordTransformationMethod.getInstance());
		}
		wifi_to_devide_application = (TextView) findViewById(R.id.wifi_to_devide_application);
		mTvSsid = (TextView) findViewById(R.id.wifi_to_device_tvSsid);
		mTvSsid.setText(mSsid);
		if (mPassWord != null && mPassWord.length > 0) {
			String string = new String(mPassWord).trim();
			mEtPw.setText(string);
			mEtPw.setTransformationMethod(PasswordTransformationMethod.getInstance());
		}

		if (isSupportLenExt&&isSupportFullChar) {
			mEtPw.setFilters(new InputFilter[] { new InputFilter.LengthFilter(63), new FullCharFilter(this), new EmojiFilter() });
		} else {
			mEtPw.setFilters(new InputFilter[] { new InputFilter.LengthFilter(31), new SpcialCharFilterWIFISET(this),new EmojiFilter() });
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		if (mCamera != null) {
			mCamera.registerIOSessionListener(this);
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		if (mCamera != null) {
			mCamera.unregisterIOSessionListener(this);
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		send = false;
	}

	@Override
	public void receiveIOCtrlData(HiCamera arg0, int arg1, byte[] arg2, int arg3) {
		if (arg0 != mCamera)
			return;

		Bundle bundle = new Bundle();
		bundle.putByteArray(HiDataValue.EXTRAS_KEY_DATA, arg2);
		Message msg = mHandler.obtainMessage();
		msg.what = HiDataValue.HANDLE_MESSAGE_RECEIVE_IOCTRL;
		msg.obj = arg0;
		msg.arg1 = arg1;
		msg.arg2 = arg3;
		msg.setData(bundle);
		mHandler.sendMessage(msg);
	}

	@Override
	public void receiveSessionState(HiCamera arg0, int arg1) {

	}

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
				case WiFiSettingActivity.SET_WIFI_END: {
					if (!send) {
						return;
					}
					dismissjuHuaDialog();
					Intent intentBroadcast = new Intent();
					intentBroadcast.setAction(HiDataValue.ACTION_CAMERA_INIT_END);
					sendBroadcast(intentBroadcast);
					Intent intent = new Intent(WifiSetToDeviceActivity.this, MainActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
				}
				break;
				case HiDataValue.HANDLE_MESSAGE_RECEIVE_IOCTRL:
					if (msg.arg2 == 0) {
						Bundle bundle = msg.getData();
						byte[] data = bundle.getByteArray(HiDataValue.EXTRAS_KEY_DATA);
						switch (msg.arg1) {
							case HiChipDefines.HI_P2P_SET_WIFI_PARAM_NEWPWD255_EXT1:
							case HiChipDefines.HI_P2P_SET_WIFI_PARAM:
								dismissjuHuaDialog();
								HiToast.showToast(WifiSetToDeviceActivity.this,getString(R.string.tips_wifi_setting));
								Intent intentBroadcast = new Intent();
								intentBroadcast.setAction(HiDataValue.ACTION_CAMERA_INIT_END);
								sendBroadcast(intentBroadcast);
								Intent intent = new Intent(WifiSetToDeviceActivity.this, MainActivity.class);
								intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
								startActivity(intent);
								break;
						}
					} else {
						switch (msg.arg1) {
							case HiChipDefines.HI_P2P_SET_WIFI_PARAM_NEWPWD255_EXT1:
							case HiChipDefines.HI_P2P_SET_WIFI_PARAM:
								dismissjuHuaDialog();
								HiToast.showToast(WifiSetToDeviceActivity.this,getString(R.string.tips_wifi_setting_fail));
								break;
						}
					}
			}
		}
	};

}
